/**
 * Tests for the model_event.js file
 * 
 * Author: Chris Casola, Michael Audi
 */

$(document).ready(function(){
	
module("Tests for Event class");
	
	test("testConstructor", function() {
		expect(6);
		var event = new Event(5, 6);
		equal(0, event.state,"State Set");
		equal(5, event.numChoices,"numChoices Set");
		equal(6, event.numRounds,"numRounds Set");
		equal(0, event.currentPlayer,"currentPlayer Set");
		equal(1, event.currentRound,"currentRound Set");
		event.question = "What is even?";
		equal("What is even?", event.question,"Checks the event question");
	});
	
	test("testAddEdge", function() {
		expect(18);
		var event = new Event(5, 6); 
		ok(event.addEdge(0, 10),"Edge added to choice[0]");
		ok(event.addEdge(1, 20),"Edge added to choice[1]");
		ok(event.addEdge(1, 30),"Edge added to choice[1]");
		equal(10, event.choices[0].rightEdges[0].vPos,"Check height of rightEdge[0] for choice[0]");
		equal(10, event.choices[1].leftEdges[0].vPos,"Check height of leftEdge[0] for chocie[1]");
		equal(20, event.choices[1].rightEdges[0].vPos,"Check height of rightEdge[0] for chocie[1]");
		equal(30, event.choices[1].rightEdges[1].vPos,"Check height of rightEdge[1] for chocie[1]");
		
		equal(3, event.currentPlayer,"Checks the currentPlayer");
		equal(1, event.currentRound,"Checks the currentRound");
		
		ok(!event.addEdge(0, 15),"Cannot add edge, too close to adjacent");
		ok(!event.addEdge(1, 10),"Cannot add edge, too close to adjacent");
		
		ok(event.addEdge(3, 20),"Edge added to choice[3]");
		ok(!event.addEdge(2, 30),"Cannot add edge, too close to adjacent");
		
		raises(function() {event.addEdge(4, 30);},"Cannot add edge, choice[4] only exists as a right bound");
		raises(function() {event.addEdge(-1, 30);},"Cannot add edge, choice[-1] does not exist");
	
		event.beginRounds();
		
		ok(event.beginRounds,"Sucessfully call beginRounds on an event");
		equal(2,event.state,"State sucessfully moved into beginRounds");
		
		event.state = 3;
		
		equal(false,event.addEdge(100, 50),"Checks adding an edge after state changed");
	});
	
	test("testCheckAdjConst", function() {
		expect(13);
		
		var event = new Event(5, 6);
		event.choices[0].addEdge(5, "right");
		
		equal(false, event.checkAdjConst(0, 0),"Two edges 5 pixels appart: test false");
		equal(false, event.checkAdjConst(1, 0),"Two edges 4 pixels appart: test false");
		equal(false, event.checkAdjConst(3, 0),"Two edges 2 pixels appart: test false");
		equal(false, event.checkAdjConst(4, 0),"Two edges 1 pixel1 appart: test false");
		equal(false, event.checkAdjConst(5, 0),"Two edges 0 pixels appart: test false");
		equal(false, event.checkAdjConst(12, 0),"Two edges 7 pixels appart: test false");
		equal(true, event.checkAdjConst(13, 0),"Two edges 8 pixels appart: test true");
		equal(true, event.checkAdjConst(14, 0),"Two edges 9 pixels appart: test true");
		equal(true, event.checkAdjConst(40, 0),"Two edges 35 pixels appart: test true");
		
		event.choices[0].addEdge(25, "right");
		equal(true, event.checkAdjConst(17, 0),"Edge at 17 between two at 5 & 25 : test true");
		equal(false, event.checkAdjConst(18, 0),"Edge at 18 between two at 5 & 25 : test false");
		equal(true, event.checkAdjConst(33, 0),"Edge at 33, 8 pixels appart from 25 : test true");
		equal(false, event.checkAdjConst(32, 0),"Edge at 33, 7 pixels appart from 25 : test false");
	});
	
	test("testFollowPath", function() {
		
		var event = new Event(3,3);
			
			event.addEdge(0,10);
			event.addEdge(1,20);
			event.addEdge(1,30);
			event.addEdge(0,40);
			event.addEdge(0,50);
			event.addEdge(1,60);
			event.addEdge(1,70);
			event.addEdge(0,80);
			event.addEdge(0,90);
				
		equal(1,event.followPath(0),"Sucessfully follows path for choice[o] to result[1]");	
		
	});
	
	test("testGetPrioritized", function() {
		
		event = new Event(6, 3);
		event.setChoiceLabel("A", 0);
		event.setChoiceLabel("B", 1);
		event.setChoiceLabel("C", 2);
		event.setChoiceLabel("D", 3);
		event.setChoiceLabel("E", 4);
		event.setChoiceLabel("F", 5);
		
		event.addEdge(0,8); // A -> B
		event.addEdge(1,57); // B -> C
		event.addEdge(0,106); // A -> B
		event.addEdge(1,157); // B -> C
		event.addEdge(2,196); // C -> D
		event.addEdge(3,256); // D -> E
		event.addEdge(4,322); // E -> F
		event.addEdge(4,134); // E -> F
		event.addEdge(3,71); // D -> E
		event.addEdge(2,87); // C -> D
		event.addEdge(3,173); // D -> E
		event.addEdge(4,190); // E -> F
		event.addEdge(2,291); // C -> D
		event.addEdge(1,319); // B -> C
		event.addEdge(0,283); // A -> B
		event.addEdge(0,371); // A -> B
		event.addEdge(3,376); // D -> E
		event.addEdge(2,395); // C -> D
		
		var result = event.getPrioritizedOrder();
		equal("D", result[0],"Check result[0]");
		equal("E", result[1],"Check result[1]");
		equal("A", result[2],"Check result[2]");
		equal("C", result[3],"Check result[3]");
		equal("F", result[4],"Check result[4]");
		equal("B", result[5],"Check result[5]");
	});
	
	test("testConstructEvent", function() {
		expect(5);
		var event = new Event(5, 6);
		
		equal(5, event.numChoices,"Checks numChoices");
		equal(6, event.numRounds,"Checks numRounds");
		equal(5, event.choices.length,"Checks the length of the array");
		ok(event.choices[0] != null,"Checks to make sure the first choice is constructed");
		ok(event.choices[4] != null,"Checks to make sure the last choice is constructed");
	});
	
	test("testSetChoiceLabel", function() {
		expect(4);
		var event = new Event(5, 6);
		
		equal("", event.choices[3].label,"Check the initial blank label of choice[3]");
		event.setChoiceLabel("Terminator", 3);
		equal("Terminator", event.choices[3].label,"Check the seeting of the label for chocie[3]");
		event.setChoiceLabel("Terminator", 4);
		equal("", event.choices[4].label,"Check the initial blank label of choice[4]");
		
		event.setChoiceLabel("",1);
		equal(false,event.validateLabel(event.choices[1].label),"Checks validation fail for a blank label");
	});
	
	test("testToJSON",function(){
		expect(2);
		var event = new Event(5,6);
		event.eventType = "open";
		event.question = "What to eat?";
		var message1 = event.toJSON();
		var message = "{\"__XMLclass\":\"createRequest\",\"type\":\"open\",\"question\":\"What to eat?\",\"numChoices\":5,\"numRounds\":6,\"__XMLchildren\":[{\"__XMLclass\":\"user\",\"name\":\"\"}]}";
		
		equal(message,JSON.stringify(message1),"Checks the JSON conversion of an event with no choice labels");
		
		event.setChoiceLabel("TEST",0);
		message1 = event.toJSON();
		message = "{\"__XMLclass\":\"createRequest\",\"type\":\"open\",\"question\":\"What to eat?\",\"numChoices\":5,\"numRounds\":6,\"__XMLchildren\":[{\"__XMLclass\":\"choice\",\"value\":\"TEST\",\"index\":\"0\"},{\"__XMLclass\":\"user\",\"name\":\"\"}]}";
		
		equal(message,JSON.stringify(message1),"Checks the JSON conversion of an event with a choice label");
	
	
	});
});